package com.cyx.service;

import com.cyx.mapper.BorrowMapper;
import com.cyx.pojo.Book;
import com.cyx.pojo.BorrowOrder;
import com.cyx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("borrowService")
public class BorrowServiceImpl implements BorrowService {

    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private BookService bookService;

    @Override
    public boolean borrowBooks(int userId, int bookId) {
        User user = userService.getUser(userId);
        Book book = bookService.getBook(bookId);
        return borrowBooks(user,book);
    }

    @Override
    @Transactional
    public boolean borrowBooks(int userId, List<Integer> bookIds) throws Exception {
        User user = userService.getUser(userId);
        for (int bookId : bookIds) {
            Book book = bookService.getBook(bookId);
            if (!borrowBooks(user,book)){
                throw new RuntimeException("借阅失败");
            }
        }
        return true;
    }
    @Override
    public boolean borrowBooks(User user, Book book) {
        if(user!=null && book!=null && book.getBookCounts()>0){
            //1、检测书目是否已被用户借阅
            List<BorrowOrder> orders = borrowMapper.getBorrowOrders(user.getId(), book.getBookId(), 1);
            if (orders!=null && orders.size()>0){
                System.out.println("用户已借阅此书，尚未归还，"+book);
                return false;
            }
            //写入借阅条目
            BorrowOrder order = new BorrowOrder();
            order.setBookId(book.getBookId());
            order.setUserId(user.getId());
            order.setBorrowTime(new Date());
            borrowMapper.addBorrowOrder(order);
            //减少库存
            book.setBookCounts(book.getBookCounts()-1);
            bookService.reduceBookStock(book.getBookId());
            return true;
        }else {
            System.out.println("用户不存在或者书目不存在或者书目库存不足");
            return false;
        }

    }


    @Override
    public boolean sendBackBooks(int userId, int bookId) throws Exception {
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(bookId);
        return sendBackBooks(userId,ids);
    }

    @Override
    @Transactional
    public boolean sendBackBooks(int userId, List<Integer> bookIds) throws Exception {
        User user = userService.getUser(userId);
        for (int bookId : bookIds) {
            Book book = bookService.getBook(bookId);
            if (!sendBackBooks(user,book)){
                throw new Exception("归还失败");
            }
        }
        return true;
    }

    @Override
    public boolean sendBackBooks(User user, Book book) {
        if(user!=null && book!=null){
            //1、检测书目是否已被用户借阅
            List<BorrowOrder> orders = borrowMapper.getBorrowOrders(user.getId(), book.getBookId(), 1);
            if (orders!=null && orders.size()>0){
                //写入借阅条目
                BorrowOrder order = orders.get(0);
                order.setSendBackTime(new Date());
                borrowMapper.updateBorrowOrder(order);
                //增加库存
                book.setBookCounts(book.getBookCounts()-1);
                bookService.increaseBookStock(book.getBookId());
                return true;

            }else{
                System.out.println("用户未借阅此书，无效归还，"+book);
                return false;
            }
        }else {
            System.out.println("用户不存在或者书目不存在");
            return false;
        }
    }
}
